<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '@/api/auth.js'

const router = useRouter()
const logoUrl = '../static/logo.jpg'

const formRef = ref()
const form = ref({
  username: '',
  password: '',
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名至少 3 位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少 6 位', trigger: 'blur' },
  ],
}

const loading = ref(false)

const onSubmit = async () => {
  await formRef.value.validate(async (valid) => {
    if (!valid) return
    loading.value = true
    try {
      const res = await login({ username: form.value.username, password: form.value.password })
      if (res?.code === '200') {
        ElMessage.success(res.msg || '登录成功')
        router.push('/admin')
      } else {
        ElMessage.error(res?.msg || '登录失败，请稍后再试')
      }
    } catch (err) {
      ElMessage.error('登录失败，请稍后再试')
    } finally {
      loading.value = false
    }
  })
}
</script>

<template>
  <div class="login-page">
    <div class="decor decor-1"></div>
    <div class="decor decor-2"></div>
    <div class="decor decor-3"></div>

    <div class="login-container">
      <div class="brand">
        <!-- <el-avatar :size="60" :src="logoUrl" class="brand-logo">MM</el-avatar> -->
      </div>

      <el-card class="login-card" shadow="always">
        <!-- <div class="card-logo">
          <img :size="74" :src="logoUrl" class="brand-logo" />
        </div> -->
        <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" placeholder="请输入管理员用户名" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" type="password" show-password placeholder="请输入密码" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="onSubmit" class="full">登 录</el-button>
          </el-form-item>
          <!-- <div class="helper">
            <el-button link @click="router.push('/home')">返回首页</el-button>
          </div> -->
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.login-page {
  position: relative;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #2F4050; /* 统一若依背景色 */
  overflow: hidden;
}

/* 背景装饰，模仿若依登录页的柔和渐变与光晕 */
.decor {
  position: absolute;
  filter: blur(80px);
  opacity: 0.6;
}
.decor-1 {
  width: 360px;
  height: 360px;
  top: -60px;
  left: -60px;
  background: radial-gradient(closest-side, #8ec5ff, transparent);
}
.decor-2 {
  width: 420px;
  height: 420px;
  bottom: -80px;
  right: -80px;
  background: radial-gradient(closest-side, #6aa8ff, transparent);
}
.decor-3 {
  width: 280px;
  height: 280px;
  bottom: 20%;
  left: 15%;
  background: radial-gradient(closest-side, #b3d7ff, transparent);
}

.login-container {
  width: 440px;
  z-index: 1;
}
.brand { display: none; }

.card-logo {
  display: flex;
  justify-content: center;
  margin: 2px 0 14px;
}
.brand-logo {
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
  border: 2px solid rgba(255, 255, 255, 0.35);
  background: rgba(255, 255, 255, 0.9);
  color: #333;
  font-weight: 600;
}
.brand-text .title {
  font-size: 20px;
  font-weight: 700;
}
.brand-text .sub {
  font-size: 12px;
  opacity: 0.9;
}

.login-card {
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.16);
  backdrop-filter: saturate(180%) blur(14px);
  border: 1px solid rgba(255, 255, 255, 0.28);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.25);
  padding: 34px 50px 34px 34px;
}

/* 玻璃态输入与标签样式 */
:deep(.login-card .el-form-item__label) {
  color: #eaf2f9;
}
:deep(.login-card .el-input__wrapper) {
  background: rgba(255, 255, 255, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.30);
  box-shadow: none;
}
:deep(.login-card .el-input__wrapper:hover),
:deep(.login-card .is-focus .el-input__wrapper) {
  background: rgba(255, 255, 255, 0.28);
  border-color: #a6c8ff;
}
:deep(.login-card .el-input__inner) {
  color: #fff;
}
:deep(.login-card .el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.75);
}

.el-form-item:last-child {
  margin-bottom: 0;
}
.el-button.full {
  width: 100%;
}
.helper {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
}
</style>